package com.mall.dto;

import lombok.Data;

import javax.validation.constraints.Min;

/**
 * 商品查询DTO
 * 
 * @author Mall Team
 */
@Data
public class ProductQueryDTO {
    
    @Min(value = 1, message = "页码必须大于0")
    private Integer pageNum = 1;
    
    @Min(value = 1, message = "页面大小必须大于0")
    private Integer pageSize = 10;
    
    private String keyword;      // 关键词搜索
    private Long categoryId;     // 分类ID
    private String brand;        // 品牌
    private Integer status;      // 状态
    private String sortBy;       // 排序字段：price, createTime, stock
    private String sortOrder;    // 排序方向：asc, desc
    
    /**
     * 获取偏移量
     */
    public Integer getOffset() {
        return (pageNum - 1) * pageSize;
    }
    
    /**
     * 获取排序SQL
     */
    public String getOrderBy() {
        if (sortBy == null || sortBy.trim().isEmpty()) {
            return "create_time DESC";
        }
        
        String order = "DESC";
        if ("asc".equalsIgnoreCase(sortOrder)) {
            order = "ASC";
        }
        
        switch (sortBy.toLowerCase()) {
            case "price":
                return "price " + order;
            case "createtime":
                return "create_time " + order;
            case "stock":
                return "stock " + order;
            default:
                return "create_time DESC";
        }
    }
}